package com.yjf.ljr.activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.yjf.ljr.R;
import com.yjf.ljr.common.CommonData;
import com.yjf.ljr.connection.IjConnect;
import com.yjf.ljr.dataclass.UpdateMobileClass;
import com.yjf.ljr.parser.RequestBuilder;
import com.yjf.ljr.parser.RequestBuilder.RequestObject;

public class BindPhoneNumberActivity extends IjActivity {

	private EditText mEtNewPhoneNumber;// 新手机号
	private Button mBtOk;// 确定
	static BindPhoneNumberActivity instance;
	private EditText mEtNewCode;// 验证码
	private Button mBtNewSendButton;// 发送按钮

	private String newMobile;
	private String md5UserBaseId;// 验证userBaseId

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		instance = this;
		setContentView(R.layout.activity_bind_phonenumber);
		initControl();
	}

	private void initControl() {
		setTitleStr("绑定手机号");
		setLeftBtnClick();
		newMobile = getIntent().getStringExtra("newMobile");
		md5UserBaseId = getIntent().getStringExtra("md5UserBaseId");

		// time = new TimeCount(60000, 1000);// 构造CountDownTimer对象
		// mEtNewPhoneNumber = (EditText) findViewById(R.id.etnewphone);
		mBtOk = (Button) findViewById(R.id.btok);
		mEtNewCode = (EditText) findViewById(R.id.etnewcodemessage);
		// mBtNewSendButton = (Button) findViewById(R.id.btnewsend);
		/*
		 * mBtNewSendButton.setOnClickListener(new OnClickListener() {
		 * 
		 * @Override public void onClick(View arg0) { // TODO Auto-generated
		 * method stub if (TextUtils.isEmpty(mEtNewCode.getText().toString())) {
		 * showToast("请输入新手机号码!!!"); } else { new
		 * SendSmsCodeTask(mEtNewPhoneNumber.getText().toString()).execute();
		 * time.start(); }
		 * 
		 * } });
		 */
		mBtOk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				/*
				 * if
				 * (TextUtils.isEmpty(mEtNewPhoneNumber.getText().toString())) {
				 * 
				 * showToast("请输入新手机号！！！"); } else
				 */
				if (TextUtils.isEmpty(mEtNewCode.getText().toString())) {
					showToast("请输入验证码！！！");
				} else {
					new UpdateMobile(mEtNewCode.getText().toString()).execute();
					showProgressDialog("正在验证手机验证码，请稍后。");
				}
			}
		});

	}

	class UpdateMobile extends AsyncTask<Void, Void, Void> {
		// private String mNewMobile;// 新电话号码
		private String mCode;// 短信验证码
		private String mFailLog;
		private UpdateMobileClass dateMobileClass = new UpdateMobileClass();;

		public UpdateMobile(String mCode) {
			// this.mNewMobile = mNewMobile;
			this.mCode = mCode;
		}

		@Override
		protected Void doInBackground(Void... arg0) {
			// TODO Auto-generated method stub
			RequestObject requestObjectMobileObject = new RequestObject();
			requestObjectMobileObject.method = "appChangeToNewPhone";
			// requestObjectMobileObject.map.put("newMobile", mNewMobile);
			requestObjectMobileObject.map.put("code", mCode);
			requestObjectMobileObject.map.put("md5UserBaseId", md5UserBaseId);
			requestObjectMobileObject.map.put("newMobile", newMobile);

			String requestMobileUrl = RequestBuilder.build(requestObjectMobileObject);
			try {
				IjConnect.doRequest(requestMobileUrl, dateMobileClass);
				if (dateMobileClass.code == null) {
					mFailLog = getResources().getString(R.string.loaddata_exception);
				} else if (!dateMobileClass.code.equals(CommonData.RESULT_SUCCESS)
						&& !("1".equals(dateMobileClass.result))) {
					mFailLog = dateMobileClass.msg;
				}
			} catch (Exception e) {
				// TODO: handle exception
				mFailLog = getResources().getString(R.string.loaddata_exception);
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			if (!TextUtils.isEmpty(mFailLog)) {
				showToast(mFailLog);
				dismissProgressDialog();
			} else {
				if (dateMobileClass.code != null && dateMobileClass.code.equals(CommonData.RESULT_SUCCESS)) {
					// TODO GET Verification code
					// showToast(dateMobileClass.msg);
					dismissProgressDialog();
					
					startActivityForResult(new Intent(BindPhoneNumberActivity.this, SuccessActivity.class),1101);
				}
			}
		}

	}

	/*
	 * class TimeCount extends CountDownTimer { public TimeCount(long
	 * millisInFuture, long countDownInterval) { super(millisInFuture,
	 * countDownInterval);// 参数依次为总时长,和计时的时间间隔 }
	 * 
	 * @Override public void onFinish() {// 计时完毕时触发
	 * mBtNewSendButton.setText("重新发送"); mBtNewSendButton.setClickable(true); }
	 * 
	 * @Override public void onTick(long millisUntilFinished) {// 计时过程显示
	 * mBtNewSendButton.setClickable(false);
	 * mBtNewSendButton.setText(millisUntilFinished / 1000 + "秒后可重新发送"); } }
	 */

	/*
	 * class SendSmsCodeTask extends AsyncTask<Void, Void, Void> { private
	 * String mobile;// 电话号码 private String mFailLog; private SendSmsCodeClass
	 * dateSendSms = new SendSmsCodeClass();
	 * 
	 * public SendSmsCodeTask(String mobile) { this.mobile = mobile; }
	 * 
	 * @Override protected Void doInBackground(Void... arg0) { // TODO
	 * Auto-generated method stub RequestObject requestObjectSms = new
	 * RequestObject(); requestObjectSms.method = "sendSmsCode";
	 * requestObjectSms.map.put("mobile", mobile);
	 * requestObjectSms.map.put("business", "cellphone");
	 * 
	 * String requestSmsUrl = RequestBuilder.build(requestObjectSms); try {
	 * IjConnect.doRequest(requestSmsUrl, dateSendSms); if (dateSendSms.code ==
	 * null) { mFailLog = getResources().getString(R.string.loaddata_exception);
	 * } else if (!dateSendSms.code.equals(CommonData.RESULT_SUCCESS)) {
	 * mFailLog = dateSendSms.msg; } } catch (Exception e) { // TODO: handle
	 * exception mFailLog =
	 * getResources().getString(R.string.loaddata_exception);
	 * e.printStackTrace(); } return null; }
	 * 
	 * @Override protected void onPostExecute(Void result) { // TODO
	 * Auto-generated method stub if ("-1".equals(dateSendSms.code)) {
	 * loginWebview(); return; } if (!TextUtils.isEmpty(mFailLog)) {
	 * showToast(mFailLog); } else { if (dateSendSms.code != null &&
	 * dateSendSms.code.equals(CommonData.RESULT_SUCCESS)) { // TODO GET
	 * Verification code showToast(dateSendSms.msg); setTitleStr("验证手机"); } } }
	 * 
	 * }
	 */
}
